﻿using System;
using System.IO;
using System.Net;
using System.Web;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace CSharp.UserControl
{
    public partial class UploadFileUC : System.Web.UI.UserControl
    {
        public static String lstLink;
        protected void Page_Load(object sender, EventArgs e)
        {
            fudInputLocal.Attributes["multiple"] = "multiple";
            fudInputLocal.Attributes["onchange"] = "UploadFile(this)";
        }

        protected void btnUploadLocal_Click(object sender, EventArgs e)
        {
            try
            {
                ResourceEO objTemp;
                KeyValueEO objKV = new KeyValueEO();
                Array strFileRemove = StringEx.Trim(hfFileRemove.Value).Split(',');
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFile postedFile = Request.Files[i];
                    if (postedFile.ContentLength > 0 && Array.IndexOf(strFileRemove, postedFile.FileName) < 0)
                    {
                        objKV = OtherEx.ConvertNumberToChar(null, ConstEx.PublicID.RESOURCE);
                        objTemp = new ResourceEO(Convert.ToInt64(objKV.K), objKV.V, Path.GetFileName(postedFile.FileName), "~/App_GlobalResources/" + objKV.V + Path.GetExtension(postedFile.FileName), null, postedFile.ContentLength, 1, 1, DateTime.Now.AddDays(1));
                        postedFile.SaveAs(Server.MapPath(objTemp.sPath));
                        (new ResourceDAO()).Insert_Update_Delete(objTemp, ConstEx.Insert_Update_Delete.Insert);
                        lblMsg.Text += string.Format("<b>{0}</b> uploaded.<br />", objTemp.sName);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
            }
        }

        protected void btnUploadGlobal_Click(object sender, EventArgs e)
        {
            try
            {
                KeyValueEO objKV = new KeyValueEO();
                objKV = OtherEx.ConvertNumberToChar(null, ConstEx.PublicID.RESOURCE);
                if (CommonsCheck.KeyValueEO_OK(objKV))
                {
                    ResourceEO objTemp = new ResourceEO(Convert.ToInt64(objKV.K), objKV.V, Path.GetFileName(txtInputGlobal.Text), "~/App_GlobalResources/" + objKV.V, txtInputGlobal.Text, 1, 1, 1, DateTime.Now);
                    using (WebClient webClient = new WebClient())
                    {
                        WebProxy proxy = new WebProxy("proxy.fis.vn", 8080);
                        proxy.Credentials = new NetworkCredential("fis\\ThangNM11", "Th@n9NM!!");
                        proxy.UseDefaultCredentials = false;
                        proxy.BypassProxyOnLocal = true;
                        webClient.Proxy = proxy;
                        webClient.DownloadFile(objTemp.sUrl, Server.MapPath(objTemp.sPath));
                    }
                    FileInfo fTemp = new FileInfo(Server.MapPath(objTemp.sPath));
                    objTemp.iSize = Convert.ToInt32(fTemp.Length);
                    (new ResourceDAO()).Insert_Update_Delete(objTemp, ConstEx.Insert_Update_Delete.Insert);
                    lblMsg.Text = "Upload thành công!";
                }
                else
                    lblMsg.Text = "Không lấy được ID";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.ToString();
            }
        }
    }
}